package com.tac.controller;

import com.github.pagehelper.PageInfo;
import com.tac.common.result.Result;
import com.tac.pojo.Comment;
import com.tac.pojo.Order;
import com.tac.pojo.vo.Page;
import com.tac.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

@RequestMapping("/order")
@CrossOrigin
@RestController
@Api(value = "订单",tags ="订单" )
public class OrderController  {
    @Autowired
    private OrderService orderService;
    /**
     * 添加订单 直接计算购物车中选中的商品 生成订单快照
     * @param
     * @return
     */
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation("添加订单从购物车下单")
   @PostMapping("/{userAddressId}/{setRemarks}")
    public Result save(@PathVariable Integer userAddressId, @PathVariable String setRemarks){
       return orderService.save(userAddressId,setRemarks);
    }

    /**
     * 添加订单2 直接从商品详情中购买 生成订单快照
     * @param
     * @return
     */
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation("添加订单之商品详情直接下单")
    @PostMapping("/{pid}")
    public Result save2(@PathVariable Integer pid,Integer quantity, Integer userAddressId,String setRemarks){
        return orderService.save2(pid,quantity,userAddressId,setRemarks);
    }
    /**
     * 根据id删除订单
     * @param oid
     * @return
     */
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @ApiOperation("根据id删除订单")
    @DeleteMapping("/{oid}")
    public Result removeById(@PathVariable Integer oid){
        return orderService.removeById(oid);
    }

    /**
     * 更新订单 （主要更新状态用）
     * @param oid
     * @return
     */
    @ApiOperation("更新订单状态,1也是付款")
    @PutMapping
    public Result updeteById(Integer oid,Integer status){
        return orderService.updeteById(oid,status);
    }
    /**
     * 用户查看订单分页 时间排序
     * @param
     * @return
     */
    @ApiOperation("用户查看订单分页时间倒序")
    @GetMapping
    public Result<Page<List<Order>>> selectAll(Integer page, Integer limit){
        return orderService.selectAll(page,limit);
    }

    /**
     * 用户根据订单状态查看订单
     * @param status
     * @return
     */
    @ApiOperation("用户根据订单状态查看订单")
    @GetMapping("/{status}/{page}/{limit}")
    public Result<Page<List<Order>>> selectByStatus(@PathVariable Integer status, @PathVariable Integer page, @PathVariable Integer limit){
        return orderService.selectByStatus(status,page,limit);
    }

    /**
     * 根据id查看详情
     * @param id
     * @return
     */
    @ApiOperation("根据id查看详情")
    @GetMapping("/{id}")
    public Result<Order> selectById(@PathVariable Integer id){
        return  orderService.selectById(id);
    }


    /**
     * 评价
     * @param comments
     * @return
     */
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping
    @ApiOperation("评价 ")
    public Result save(@Valid @RequestBody List<Comment> comments){
         return orderService.save(comments);
    }
    //---------------------------------------------------------------------------------------------
    /**
     * 管理员查看所有订单
     * @return
     */
    @GetMapping("/selectAll")
    @ApiOperation("管理员查看所有订单")
    public Result<PageInfo<Order>> selectAll2(Integer pageNum, Integer pageSize){
        return orderService.selectAll2(pageNum,pageSize);
    }

    /**
     * 管理员分类型查询订单等功能
     * @return
     */
    @GetMapping("/selectByStatu/{status}")
    @ApiOperation("管理员分类型查询订单等功能")
    public Result<PageInfo<Order> >  selectByStatu2(@PathVariable Integer status,Integer pageNum,Integer pageSize){
        return orderService.selectByStatu2(status,pageNum,pageSize);
    }

}

